package com.huisou.basedataservice;

import com.huisou.domain.Category;
import com.huisou.orm.HibernateDao;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

/**
 * 产品分类
 * 
 * @description
 * 
 * @author 张锋石 姚卫国
 * @email dqywg@hotmail.com
 * @create 2012-4-1 上午11:11:41
 */
@Repository
public class CategoryDao extends HibernateDao<Category, Integer> {
	protected Logger				log						= LoggerFactory.getLogger(getClass());
	@Resource
	RedisTemplate<String, Object>	redisTemplate;
	@Resource
	private StringRedisTemplate		stringRedisTemplate;
	
	private static final String		SALE_CATES_CACHE_NAME	= "saleCategoriesCache";																											// 产品供应类别
	private static final String		BUY_CATES_CACHE_NAME	= "buyCategoriesCache";																											// 产品供应类别
																																																
	private final String			tophql					= "select new Category(id ,name,parentId) from Category where parentId is null and typeId=? and status=0 order by sortOrder asc ";
	private final String			hql						= "select new Category(id ,name,parentId) from Category where parentId=? and status=0 order by sortOrder asc ";
	
	/**
	 * 获取所有
	 */
	public List<Category> getAll() {
		String key = RedisConstants.KEY_PRODUCT_CATEGORY_LIST_ALL;
		
		List<Category> listCategory = null;
		try {
			listCategory = (List<Category>) redisTemplate.opsForValue().get(key);
		}
		catch (Exception e) {
			log.info("取Category出错:" + e.getMessage());
		}
		if (listCategory == null) {
			try {
				listCategory = find("select new Category(id ,name,parentId) from Category  where stauts=0");
				redisTemplate.opsForValue().set(key, listCategory);
			}
			catch (DataAccessException e) {
				log.error("获取顶级行业类别列表出错.");
				return null;
			}
		}
		return listCategory;
	}
	
	/**
	 * 获取行业顶级类别列表
	 * 
	 * @param type
	 *            类型 获取供应分类、加工分类 、招商分类等
	 * @return
	 */
	public List<Category> getTopCateList(Integer type) {
		String key = RedisConstants.KEY_PRODUCT_CATEGORY_LIST_TYPE + "_" + type;
		
		List<Category> listCategory = null;
		try {
			listCategory = (List<Category>) redisTemplate.opsForValue().get(key);
		}
		catch (Exception e) {
			log.info("取Category出错:" + e.getMessage());
		}
		if (listCategory == null) {
			try {
				listCategory = find(tophql, type);
				redisTemplate.opsForValue().set(key, listCategory);
			}
			catch (DataAccessException e) {
				log.error("获取顶级行业类别列表出错.");
				return null;
			}
		}
		return listCategory;
	}
	
	/**
	 * 获取产品供应类别
	 * 
	 * @param parentId
	 * @return
	 */
	public List<Category> getChilds(Integer parentId) {
		return getChilds(parentId, 2, SALE_CATES_CACHE_NAME);
	}
	
	/**
	 * 获取产品求购类别
	 * 
	 * @param parentId
	 * @return
	 */
	public List<Category> getBuyCates(Integer parentId) {
		return getChilds(parentId, 2, BUY_CATES_CACHE_NAME);
	}
	
	/**
	 * 第一次从数据库中获得子分类列表， 之后都从内存中读取
	 * 
	 * @param parentId
	 * @return
	 */
	public List<Category> getChilds(Integer parentId, int posInx, String cacheName) {
		String key = RedisConstants.KEY_PRODUCT_CATEGORY_LIST_PID + "_" + parentId;
		List<Category> listCategory = null;
		try {
			listCategory = (List<Category>) redisTemplate.opsForValue().get(key);
		}
		catch (Exception e) {
			log.info("取Category出错:" + e.getMessage());
		}
		if (listCategory == null) {
			try {
				listCategory = this.find(hql, parentId);
				redisTemplate.opsForValue().set(key, listCategory);
			}
			catch (DataAccessException e) {
				log.error("获取顶级行业类别列表出错.");
				return null;
			}
		}
		return listCategory;
	}
	
	public String getCatsNameByCode(String code) {
		String key = RedisConstants.KEY_PRODUCT_CATEGORY_SIGNLE_NAME + "_" + code;
		String categoryName = null;
		try {
			categoryName = stringRedisTemplate.opsForValue().get(key);
		}
		catch (Exception e) {
			log.info("取Category出错:" + e.getMessage());
		}
		if (StringUtils.isBlank(categoryName)) {
			try {
				categoryName = this.findUniqueBy("code", code).getName();
				stringRedisTemplate.opsForValue().set(key, categoryName);
			}
			catch (DataAccessException e) {
				log.error("获取顶级行业类别列表出错.");
				return null;
			}
		}
		
		return categoryName;
	}
}
